package com.wwj.app.v1;

import com.wwj.core.wawa.service.IPlayerRechargeService;
import com.wwj.framework.controller.BaseController;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/v1/app/pay")
public class AppPayController extends BaseController {

    @Resource
    private IPlayerRechargeService playerRechargeService;

    /**
     * 支付回调地址
     * @param request
     * @return
     */
    @RequestMapping(value = "/wx/notify")
    public String notify(HttpServletRequest request) throws IOException {
        return playerRechargeService.dealWxRechargeComplete(request);
    }

    @RequestMapping("/recharge")
    @ResponseBody
    public ReturnData recharge(@RequestParam Integer playerId, @RequestParam Integer itemId, @RequestParam int payType,
                               @RequestParam(required = false) Integer couponId) {
        ReturnData returnData = new ReturnData();
        try {
            Map<String, Object> result = playerRechargeService.dealPlayerRechargeByApp(playerId, itemId, payType, couponId);
            if(result == null) {
                returnData.failure("无法支付，联系客服");
                return returnData;
            }

            returnData.success(result);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/freight")
    @ResponseBody
    public ReturnData freight(@RequestParam Integer playerId, @RequestParam int payType) {
        ReturnData returnData = new ReturnData();
        try {
            Map<String, Object> result = playerRechargeService.dealPlayerRechargeByApp(playerId, -1, payType, -1);
            if(result == null) {
                returnData.failure("无法支付，联系客服");
                return returnData;
            }

            returnData.success(result);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/cancel")
    @ResponseBody
    public ReturnData cancel(@RequestParam Integer playerId, @RequestParam String tradeNo) {
        ReturnData returnData = new ReturnData();
        try {
            playerRechargeService.dealCancelPlayerRecharge(tradeNo);
            returnData.success();
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }
}
